﻿#include "../head/Bank.hh"
#include <iostream>
#include <iomanip>

void Bank::addCustomer(std::string name) {
    // 检查客户姓名是否已存在
    if (!checkCustomerName(name)) {
        std::cout << "Error: Customer name already exists.\n";
        return;
    }
    std::string savingsAccountId;
    std::string checkingAccountId;
    do {
        savingsAccountId = SavingsAccount::generateAccountID();
    } while (!checkAccountId(savingsAccountId));

    do{
        checkingAccountId = CheckingAccount::generateAccountID();
    } while (!checkAccountId(checkingAccountId));

    Customer* customer = new Customer(name, savingsAccountId, checkingAccountId);

    std::cout << "Customer " << name << " successfully registered!\n";
    std::cout << "Two bank accounts have been created:\n";
    std::cout << "  Savings  Account: " << customer->getSavingsAccount()->getAccountId() << std::endl;
    std::cout << "  Checking Account: " << customer->getCheckingAccount()->getAccountId() << std::endl;

    customers.push_back(customer);  // 添加新客户
}


Customer* Bank::findCustomer(const std::string& name) {
    for (auto* customer : customers) {
        if (customer->getName() == name) {
            return customer;
        }
    }
    return nullptr;
}

void Bank::showAllAccounts() const {
    long long totalBalance = 0;
    int totalAccounts = 0;
    int totalCustomers = (int )customers.size(); // 总客户数

    std::cout << "===== All Bank Accounts =====\n";

    // 显示储蓄账户
    std::cout << "\nSAVINGS ACCOUNTS:\n";
    for (const auto* customer : customers) {
        const auto* savingsAccount = customer->getSavingsAccount();
        std::cout << totalAccounts + 1 << ". ID: " << savingsAccount->getAccountId()
            << "  |  Owner: "  << std::setw(20)<< std::left <<customer->getName()
            << "  |  Balance: ￥"  << savingsAccount->showBalance() << "\n";
        totalBalance += savingsAccount->getBalance();
        totalAccounts++;
    }

    // 显示支票账户
    int tmp = totalAccounts;
    totalAccounts = 0;
    std::cout << "\nCHECKING ACCOUNTS:\n";
    for (const auto* customer : customers) {
        const auto* checkingAccount = customer->getCheckingAccount();
        std::cout << totalAccounts + 1 << ". ID: "  << checkingAccount->getAccountId()
            << "  |  Owner: " << std::setw(20)<< std::left << customer->getName()
            << "  |  Balance: ￥"<< checkingAccount->showBalance() << "\n";
        totalBalance += checkingAccount->getBalance();
        totalAccounts++;
    }

    // 显示总结信息
    std::cout << "\nTotal Accounts: " << totalAccounts+tmp << "\n";
    std::cout << "Total Customers: " << totalCustomers << "\n";
    std::cout << "Total Balance: ￥" << tools::amountdisplay(totalBalance) << "\n";
}

const std::vector<Customer*>& Bank::getCustomers() const {
    return customers;
}

// 根据账户ID查找账户
Account* Bank::findAccountById(const std::string& accountId) {
    for (const auto& customer : customers) {
        if (customer->getCheckingAccount()->getAccountId() == accountId)
            return customer->getCheckingAccount();
        if (customer->getSavingsAccount()->getAccountId() == accountId)
            return customer->getSavingsAccount();
    }
    return nullptr;  // 未找到账户
}

// 根据账户获取客户
Customer* Bank::findCustomerByAccount(Account* account) {
    for (const auto& customer : customers) {
        if (customer->getCheckingAccount() == account || customer->getSavingsAccount() == account) {
            return customer;
        }
    }
    return nullptr;
}

bool Bank::checkCustomerName(std::string name)
{
    for (auto* customer : customers) {
        if (customer->getName() == name) {
            return false;
        }
    }
    return true;
}

bool Bank::checkAccountId(std::string accountid)
{
    for (auto* customer : customers) {
        if (customer->getCheckingAccount()->getAccountId() == accountid || customer->getSavingsAccount()->getAccountId() == accountid ){
            return false;
        }
    }
    return true;
}
